package com.jims.api.v1.controller.common;

import com.jims.master.common.data.Message;
import com.jims.master.common.utils.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 发送短信/验证验证码
 * @author WangYq
 * @version 2017-5-15
 */
@Controller
@RequestMapping(value = "/mobile")
public class MobileController {
    /**
     * @Title: validSendSmsV100
     * @Description: （发送手机验证码）
     * @param mobile
     * @return java.lang.String
     * @author WangYQ
     * @date 2017-11-17
     * @throws
     */
    @RequestMapping(value = "V100/validSendSms",method ={RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String validSendSms(@RequestParam(required=true) String mobile, HttpServletRequest request) {
        Message msg=new Message();
        int validateNum=(int)(Math.random()*(9999-1000+1))+1000;
        String result = "00";
        /*result=SmsModule.zccode(mobile,validateNum+"");*/
        result = SmsModule.dypassword(mobile, validateNum+"", CommonUtil.getValue("servicephone"));
        CacheUtils.put("validateNum_" + mobile, validateNum + "");
        CacheUtils.put("phoneNum_" + mobile, mobile + "");
        msg.setStatus(result);
        return SecurityUtil.encryptMsgData(msg);
    }

    /**
     * @Title: checkCodeMethodV100
     * @Description: （校验手机验证码）
     * @param mobile
     * @param mobileCode
     * @return java.lang.String
     * @author WangYQ
     * @date 2017-11-19
     * @throws
     */
    @RequestMapping(value = "V100/checkCodeMethod",method ={RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    private String checkCodeMethod(@RequestParam(required=true) String mobile,@RequestParam(required=true) String mobileCode){
        Message msg=new Message();
        String validateNum=(String)CacheUtils.get("validateNum_"+mobile);
        String phoneNum=(String)CacheUtils.get("phoneNum_"+mobile);
        if(StringUtils.isBlank(validateNum) || StringUtils.isBlank(phoneNum)){
            msg.setErrCode("414");
            msg.setErrMsg("您还没有进行手机验证，请手机验证后在进行下一步操作");
        }
        if(!phoneNum.equals(mobile)){
            msg.setErrCode("414");
            msg.setErrMsg("非法操作");
        }
        if(validateNum.equals(mobileCode)){
            msg.setStatus("200");
            msg.setData("验证成功");
        }else{
            msg.setErrCode("414");
            msg.setErrMsg("验证码有误，请重新填写");
        }
        return SecurityUtil.encryptMsgData(msg);
    }
}
